package com.infodms.dms.dao.claim.oldPart;

import java.sql.ResultSet;
import java.util.Date;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.bean.ClaimOldPartOutPreListBean;
import com.infodms.dms.bean.ClaimOldPartOutStoreLogBean;
import com.infodms.dms.common.Utility;
import com.infodms.dms.dao.common.BaseDao;
import com.infodms.dms.po.TtAsWrPartStockLogPO;
import com.infodms.dms.po.TtAsWrPartStockPO;
import com.infodms.dms.util.sequenceUitl.SequenceManager;
import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.po3.bean.PO;
import com.infoservice.po3.bean.PageResult;

/**
 * 类说明：索赔旧件出库Dao
 * 作者：  赵伦达
 */
@SuppressWarnings("unchecked")
public class ClaimOldPartOutStorageDao extends BaseDao{
	
    public static Logger logger = Logger.getLogger(ClaimOldPartOutStorageDao.class);
	
	private static final ClaimOldPartOutStorageDao dao = null;
	
	public static final ClaimOldPartOutStorageDao getInstance() {
	   if(dao==null) return new ClaimOldPartOutStorageDao();
	   return dao;
	}
	/**
	 * Function：索赔旧件出库--出库记录查询
	 * @param  ：	
	 * @return:		@param params
	 * @return:		@param curPage
	 * @return:		@param pageSize
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-18
	 */
	public PageResult<ClaimOldPartOutStoreLogBean> getOutStorelogList(Map params,int curPage, int pageSize){
		String company_id=ClaimTools.dealParamStr(params.get("company_id"));
		String supply_name=ClaimTools.dealParamStr(params.get("supply_name"));//查询条件--供应商简称
		String out_start_date=ClaimTools.dealParamStr(params.get("out_start_date"));//查询条件--配件代码
		String out_end_date=ClaimTools.dealParamStr(params.get("out_end_date"));//查询条件--配件代码
		String part_name=ClaimTools.dealParamStr(params.get("part_name"));//查询条件--配件名称
		String part_code=ClaimTools.dealParamStr(params.get("part_code"));//查询条件--配件代码
		String yieldly = ClaimTools.dealParamStr(params.get("yieldly"));//查询条件--产地
		String yieldlys = ClaimTools.dealParamStr(params.get("yieldlys"));//用户拥有的产地权限
		
		StringBuffer sqlStr= new StringBuffer();
		sqlStr.append("select tps.short_name supply_name,tawps.part_code,tppb.part_name,\n" );
		sqlStr.append("tawpsl.out_before_amount,tawpsl.out_amount,\n" );
		sqlStr.append("tawpsl.out_before_amount-tawpsl.out_amount remain_amount,\n" );
		sqlStr.append("to_char(tawpsl.out_date,'YYYY-MM-DD') out_date,tawps.yieldly\n" );
		sqlStr.append("from TT_AS_WR_PART_STOCK tawps,TT_AS_WR_PART_STOCK_LOG tawpsl,\n" );
		sqlStr.append("TM_PT_SUPPLIER tps,\n" );
		sqlStr.append("Tm_Pt_Part_Base tppb\n" );
		sqlStr.append("where tawps.producer_id=tawpsl.producer_id\n" );
		sqlStr.append("and tawps.part_id=tawpsl.part_id\n" );
		sqlStr.append("and tawpsl.producer_id=tps.supplier_id(+)\n" );
		sqlStr.append("and tawpsl.part_id=tppb.part_id(+)\n");
		sqlStr.append("and tawps.oem_company_id="+company_id+"\n");
		//sqlStr.append("and tppb.oem_company_id="+company_id+"\n");
		sqlStr.append("and tawps.yieldly in ("+yieldlys+")\n" );
		if(Utility.testString(supply_name)){
			sqlStr.append("and tps.short_name like '%"+supply_name.replaceAll("'", "\''")+"%'\n" );
		}
		if(Utility.testString(out_start_date)){
			sqlStr.append(" and tawpsl.out_date>=to_date('" + out_start_date+ " 00:00:00','YYYY-MM-DD HH24:mi:ss')\n");
		}
		if(Utility.testString(out_end_date)){
			sqlStr.append(" and tawpsl.out_date<=to_date('" + out_end_date+ " 23:59:59','YYYY-MM-DD HH24:mi:ss')\n");
		}
		if(Utility.testString(part_code)){
			sqlStr.append("and tawps.part_code like '%"+part_code.replaceAll("'", "\''")+"%'\n" );
		}
		if(Utility.testString(part_name)){
			sqlStr.append("and tppb.part_name like'%"+part_name.replaceAll("'", "\''")+"%'\n" );
		}
		if(Utility.testString(yieldly)){
			sqlStr.append("and tawps.yieldly ='"+yieldly+"'\n" );
		}
		sqlStr.append("order by tawpsl.id desc\n" );
		
		PageResult<ClaimOldPartOutStoreLogBean> pr=pageQuery(ClaimOldPartOutStoreLogBean.class, sqlStr.toString(), null, pageSize, curPage);
		return pr;
	}
	/**
	 * Function：出库新增--查询符合出库条件的记录
	 *           查询表--TT_AS_WR_PART_STOCK
	 * @param  ：	
	 * @return:		@param params
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-17
	 */
	public PageResult<ClaimOldPartOutPreListBean> getPreOutStoreList(Map params,int curPage, int pageSize){
		String company_id=ClaimTools.dealParamStr(params.get("company_id"));
		String supply_name=ClaimTools.dealParamStr(params.get("supply_name"));//查询条件--供应商简称
		String part_code=ClaimTools.dealParamStr(params.get("part_code"));//查询条件--配件代码
		String part_name=ClaimTools.dealParamStr(params.get("part_name"));//查询条件--配件名称
		String yieldly = ClaimTools.dealParamStr(params.get("yieldly"));//查询条件--产地
		String yieldlys = ClaimTools.dealParamStr(params.get("yieldlys"));//用户拥有的产地权限
		
		StringBuffer sqlStr=new StringBuffer();
		
		sqlStr.append("select tawps.id,tps.short_name supply_name,\n" );
		sqlStr.append("tawps.part_code,tppb.part_name,tawps.return_amount-tawps.out_amount return_amount,\n" );
		sqlStr.append("0 out_amount,tawps.yieldly\n" );
		sqlStr.append("from TT_AS_WR_PART_STOCK tawps,\n" );
		sqlStr.append("TM_PT_SUPPLIER tps,\n" );
		sqlStr.append("Tm_Pt_Part_Base tppb\n" );
		sqlStr.append("where tawps.producer_id=tps.supplier_id(+)\n" );
		sqlStr.append("and tawps.part_id=tppb.part_id(+)\n" );
		//sqlStr.append("and tps.is_del=0\n" );去除厂商是否删除标志
		sqlStr.append("and tppb.is_del=0\n" );
		sqlStr.append("and tawps.oem_company_id="+company_id+"\n");
		//sqlStr.append("and tppb.oem_company_id="+company_id+"\n");
		sqlStr.append("and tawps.yieldly in ("+yieldlys+")\n" );
		if(Utility.testString(supply_name)){
			sqlStr.append("and tps.short_name like '%"+supply_name.replaceAll("'", "\''")+"%'\n" );
		}
		if(Utility.testString(part_code)){
			sqlStr.append("and tawps.part_code like '%"+part_code.replaceAll("'", "\''")+"%'\n" );
		}
		if(Utility.testString(part_name)){
			sqlStr.append("and tppb.part_name like '%"+part_name.replaceAll("'", "\''")+"%'\n" );
		}
		if(Utility.testString(yieldly)){
			sqlStr.append("and tawps.yieldly ='"+yieldly+"'\n" );
		}
		
		sqlStr.append(" and tawps.return_amount-tawps.out_amount>0");
		sqlStr.append("order by tawps.id desc");
		
		PageResult<ClaimOldPartOutPreListBean> pr=pageQuery(ClaimOldPartOutPreListBean.class, sqlStr.toString(), null, pageSize, curPage);
		return pr;
	}
	/**
	 * Function：出库操作
	 *           修改旧件库存表信息，新增出库记录表
	 * @param  ：	
	 * @return:		@param request 
	 * @throw：	
	 * LastUpdate：	2010-6-18
	 */
	public int saveOutOfStoreLogOper(RequestWrapper request,AclUserBean logonUserBean){
    	String idStr=request.getParamValue("idStr");
    	StringBuffer sqlStr=new StringBuffer();
    	int updateNum=0;
    	int outNum=0;
    	if(idStr!=null&&!"".equals(idStr)){
    		String[] arr=idStr.split(",");
    		for(int count=0;count<arr.length;count++){
    			outNum=Integer.parseInt(request.getParamValue("outStoreNum"+arr[count]));
    			sqlStr.delete(0, sqlStr.length());
    			sqlStr.append("select * from TT_AS_WR_PART_STOCK where id="+arr[count]);
    			PageResult<TtAsWrPartStockPO> pr=pageQuery(TtAsWrPartStockPO.class, sqlStr.toString(), null, 10, 1);
    			if(pr!=null&&pr.getTotalRecords()>0){
    				//插入出库记录表
    				TtAsWrPartStockPO existOutInfo=(TtAsWrPartStockPO)pr.getRecords().get(0);
    				TtAsWrPartStockLogPO insertObj=new TtAsWrPartStockLogPO();
    				insertObj.setId(Long.parseLong(SequenceManager.getSequence("")));
    				insertObj.setProducerId(existOutInfo.getProducerId());
    				insertObj.setPartId(existOutInfo.getPartId());
    				insertObj.setReturnAmount(existOutInfo.getReturnAmount());
    				insertObj.setOutBeforeAmount(existOutInfo.getReturnAmount()-existOutInfo.getOutAmount());
    				insertObj.setOutAmount(outNum);
    				insertObj.setOutDate(new Date());
    				insertObj.setYieldly(existOutInfo.getYieldly());
    				insertObj.setCreateDate(new Date());
    				insertObj.setCreateBy(logonUserBean.getUserId());
    				insert(insertObj);
    				//修改旧件库存表信息
    				TtAsWrPartStockPO idObj=new TtAsWrPartStockPO();
    				idObj.setId(existOutInfo.getId());
    				TtAsWrPartStockPO vo=new TtAsWrPartStockPO();
    				vo.setOutAmount(existOutInfo.getOutAmount()+outNum);
    				vo.setUpdateBy(logonUserBean.getUserId());
    				vo.setUpdateDate(new Date());
    				updateNum+=update(idObj, vo);
    			}
    		}
    	}
    	return updateNum;
    }
	/**
	 * Function：全部出库操作
	 *           修改旧件库存表信息，新增出库记录表
	 * @param  ：	
	 * @return:		@param request 
	 * @throw：	
	 * LastUpdate：	2010-6-18
	 */
	public int saveAllOutOfStoreLogOper(RequestWrapper request,AclUserBean logonUserBean){
    	String idStr=request.getParamValue("idStr");
    	StringBuffer sqlStr=new StringBuffer();
    	int updateNum=0;
    	int outNum=0;
    	if(idStr!=null&&!"".equals(idStr)){
    		String[] arr=idStr.split(",");
    		for(int count=0;count<arr.length;count++){
    			sqlStr.delete(0, sqlStr.length());
    			sqlStr.append("select * from TT_AS_WR_PART_STOCK where id="+arr[count]);
    			PageResult<TtAsWrPartStockPO> pr=pageQuery(TtAsWrPartStockPO.class, sqlStr.toString(), null, 10, 1);
    			if(pr!=null&&pr.getTotalRecords()>0){
    				//插入出库记录表
    				TtAsWrPartStockPO existOutInfo=(TtAsWrPartStockPO)pr.getRecords().get(0);
    				outNum=existOutInfo.getReturnAmount()-existOutInfo.getOutAmount();
    				TtAsWrPartStockLogPO insertObj=new TtAsWrPartStockLogPO();
    				insertObj.setId(Long.parseLong(SequenceManager.getSequence("")));
    				insertObj.setProducerId(existOutInfo.getProducerId());
    				insertObj.setPartId(existOutInfo.getPartId());
    				insertObj.setReturnAmount(existOutInfo.getReturnAmount());
    				insertObj.setOutBeforeAmount(outNum);
    				insertObj.setOutAmount(outNum);
    				insertObj.setOutDate(new Date());
    				insertObj.setCreateDate(new Date());
    				insertObj.setCreateBy(logonUserBean.getUserId());
    				insert(insertObj);
    				//修改旧件库存表信息
    				TtAsWrPartStockPO idObj=new TtAsWrPartStockPO();
    				idObj.setId(existOutInfo.getId());
    				TtAsWrPartStockPO vo=new TtAsWrPartStockPO();
    				vo.setOutAmount(existOutInfo.getOutAmount()+outNum);
    				vo.setUpdateBy(logonUserBean.getUserId());
    				vo.setUpdateDate(new Date());
    				updateNum+=update(idObj, vo);
    			}
    		}
    	}
    	return updateNum;
    }
	@Override
	protected PO wrapperPO(ResultSet rs, int idx) {
		return null;
	}
	
}
